package cn.xdd._05ConfirmMsg;

import cn.xdd.utils.RabbitMqUtils;
import cn.xdd.utils.SleepUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @author user_xdd
 * @Date 2022-05-10 20:36
 * @description 设置不公平开发basicQos
 * @return
 */
public class Comsumer {
    public static final String ACK_QUEUE_NAME = "MY_QUEUE";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
//        设置为不公平开发
        int prefetchcount = 100;
        channel.basicQos(prefetchcount);
        //消息消费的时候如何处理消息
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            SleepUtils.sleep(1);
            System.out.println("接收到消息:" + message);
        /**
         *	1.消息标记 tag
         *	2.是否批量应答未应答消息
         */                    // 交付   属性        获得标签
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
        };
//采用手动应答
        boolean autoAck = false;
        channel.basicConsume(ACK_QUEUE_NAME, autoAck, deliverCallback,(consumerTag) -> {
            System.out.println(consumerTag + "消费者取消消费接口回调逻辑");
        });
    }

}

